<template>
  <view class="login">
    <view class="" />
    <u-form ref="form1" class="login-form" :model="form">
      <u-form-item label="姓名" right-icon="lock-fill"
        ><u-input v-model="form.name" placeholder="账号"
      /></u-form-item>
      <u-form-item label="密码" right-icon="lock-fill"
        ><u-input v-model="form.intro" type="password" placeholder="密码"
      /></u-form-item>
    </u-form>
    <u-button type="primary" @click="handleLogin">登录</u-button>
  </view>
</template>

<script>
import { title } from '@/configs/index'

export default {
  data() {
    return {
      form: {},
      title,
      agree: false,
      loginCode: ''
    }
  },
  onShow() {
    this.getLoginCode()
  },
  methods: {
    getLoginCode() {
      uni.login({
        provider: 'weixin', // 使用微信登录
        success: (res) => {
          console.log('handleBeforeLogin.res', res)
          this.loginCode = res.code
        }
      })
    },

    async handleLogin({ detail = {} } = {}) {
      console.log('login.detail', detail)

      const params = {
        ...detail,
        code: this.loginCode,
        encryptedData: detail.encryptedData,
        iv: detail.iv,
        mockData: {
          token: 'mock-token'
        }
      }

      const res = await this.$req.mockRequest(params)

      if (res.code === '0000') {
        this.$store.userStore.setToken(res.data.token)
        await this.$toast('登录成功', { type: 'success' })
        this.$Router.push({ path: '/home' })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  padding: 80rpx;
  .login-form {
  }
}
</style>
